<?php

/**
* chuixue 漫画操作类
*/

class chuixue {

	public static $action = 'index';
	public $param;
	
	public function __construct() {
		$this->loader();
	}
	
	protected function loader() {
		loadClass('xindm');
		//loadClass('snoopy');
	}
	
	public function index() {
		
		$location = 'http://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
		$location = preg_replace('/:\d+/i', '', $location);
		new Xindm_tool(NULL);
		include template('front'); //WEB_PATH . '/template/html/front.phtml';
	}
	
	public function checkDown() {
		$param = $this->setParam();
		$xindmComic = new Xindm_tool($param);
		$xindmComic->checkPicAction();
	}
	
	public function down() {
		//header("Content-type: text/html; charset=gb2312"); // 
		$param = $this->setParam();
		$xindmComic = new Xindm_tool($param);
		include template('down'); //WEB_PATH . '/template/html/down.phtml';
		$xindmComic->downAction();
	}
	
	public function read() {
		header("Content-type: text/html; charset=utf-8"); // 
		new Xindm_tool(NULL);
		include template('read'); //WEB_PATH . '/template/html/read.phtml';
	}
	
	public function readVol() {
		$param = $this->setParam();
		$xindmComic = new Xindm_tool($param);
		$_data = $xindmComic->readVolAction();
		if (!isset($_GET['rids'])) die('false');
		$rids = $_GET['rids'][0] - 1;
		$data = $_data[$rids]['pics'];
		$json = JSON($data);
		echo $json;
	}
	
	public function search() {
		$searchName = isset($_GET['searchName']) ? $_GET['searchName'] : '';
		$page = isset($_GET['page']) && $_GET['page'] > 0 ? $_GET['page'] - 0 : 1;
		$stype = isset($_GET['searchType']) && $_GET['searchType'] == 1 ? 1 : 0; // $stype [0]电脑 [1]手机
		$searchName = rawurldecode($searchName);
		//$searchName = mb_convert_encoding($searchName, 'CP936', 'UTF-8'); // GB2312

		$length = strlen($searchName);
		if ($length < 1 || $length > 50) die('{"status": "false", "mag": "String length must be greater than 50 and less than 2"}');
		$json = Xindm_tool::loadStaticMethod('searchComic', array($searchName, $page, $stype)); //Xindm_tool::searchComic($searchName, $page, $stype);
		echo $json ? $json : '{"status": "false", "msg": "Unkown Error ! ", "count": "0"}';
	}
	
	public function random() {
		$comicNamesArr = Xindm_tool::loadStaticMethod('getAllComicNames'); // Xindm_tool::getAllComicNames();
		$comicNamesArr || die('{"status": "false", "msg": "no res ! ", "count": "0"}');
		$jsonArr = array(
			'status' => 'true',
			'msg' => 'ok',
			'nums' => count($comicNamesArr),
			'comicNames' => $comicNamesArr
		);
		$comicNamesJson = JSON($jsonArr);
		echo $comicNamesJson;
	}
	
	public function showVols() {
		$time = time();
		$comicName = isset($_GET['comicName']) ? $_GET['comicName'] : '';
		$encodeName = rawurlencode(rawurldecode($comicName));
		if ($encodeName == '') die('{"status": "false", "mag": "comicName is null ! ('.$comicName.') "}');
		$json = Xindm_tool::loadStaticMethod('getComicInfo', array($encodeName)); // Xindm_tool::getComicInfo($encodeName);
		echo json_encode($json, JSON_UNESCAPED_UNICODE);//JSON($json);
	}
	
	public function downloadControl() {
		$comic = isset($_GET['comic']) && trim($_GET['comic']) != '' ? trim($_GET['comic']) : FALSE;
		$comic || die('{"status": "false", "msg": "comic is undefined !"}');
		$json = Xindm_tool::loadStaticMethod('downloadControl', array($comic)); // Xindm_tool::downloadControl($comic);
		echo $json;
	}
	
	protected function setParam() {
		$rids = isset($_GET['r']) && $_GET['r'] != '' && preg_match('/^(?:\d+,?)+$/', $_GET['r']) ? $_GET['r'] : (isset($_GET['rids']) && $_GET['rids'] && is_array($_GET['rids']) ? implode(',', $_GET['rids']) : FALSE);
		return $this->param = array(
			'comicname' => isset($_GET['n']) && $_GET['n'] != '' ? $_GET['n'] : '',
			'ridtype' => isset($_GET['t']) && $_GET['t'] == 'any' ? 'any' : 'all',
			'rids' => $rids,
			'savetype' => isset($_GET['s']) && $_GET['s'] == 'database' ? 'database' : 'file',
			'runmode' => isset($_GET['u']) && $_GET['u'] == 'background' ? 'background' : 'foreground',
			'checkinfo' => isset($_GET['c']) && $_GET['c'] ? 1 : 0,
			'zip' => isset($_GET['z']) && $_GET['z'] == '1' ? 1 : 0,
			''
		);
	}
}